package com.blog.common.interceptor;

import com.blog.common.constant.Constants;
import com.blog.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String userToken = request.getHeader(Constants.USER_TOKEN_KEY);
if(userToken==null){
    response.setStatus(401);
    return false;
}
        Claims claims= (Claims) JwtUtils.parseToken(userToken);
if(claims==null){
    response.setStatus(401);
    return false;
}
return true;
    }
}
